<?php

class ReportsController extends Controller
{
    
    /**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}
    
	public function actionIndex()
	{
	    // Find all bill current month
        $this->_report(date('m-Y'));
	}
    
    
    public function actionDetail($id)
    {
        $bill = Bill::model()->findByPk($id);
        if($bill===null)
			throw new CHttpException(404,'The requested page does not exist.');
        $this->renderPartial('_detail',array('data' => unserialize($bill->data)));
    }
    
    public function actionImport($from,$to)
    {
        //var_dump($_POST);
        $model = $this->_import($from,$to);
        $items = Bill::model()->findAll($model->getCriteria());
        $total_money_import = 0;
        foreach($items as $item)
        {
            $money = unserialize($item->data);
            $total_money_import += $money['payment'];
        };
        $this->renderPartial('_import',array(
            'model' => $model
            ,'money_import' => $total_money_import,
            )
        );
    }
    
    public function actionExport($from,$to)
    {
        $model = $this->_export($from,$to);
        $items = SupplyItems::model()->findAll($model->getCriteria());
        $total_money_export = 0;
        foreach($items as $item)
        {            
            $money = $item->price;            
            $total_money_export += $money;
        }
        $this->renderPartial('_export',
                        array(
                        'model' => $model,
                        'money_export' => $total_money_export,
                        )
        );
    }
    
    public function actionView($month)
    {     
        $this->_report($month);
    }
    
    
    
    // find import money from date to date
    
    protected function _export($from,$to)
    {       
        $criteria = new CDbCriteria;
        $criteria->condition='created >= :from AND created <= :to';
        $criteria->params=array(':to'=>date('Y-m-d',strtotime($to)), ':from'=>date('Y-m-d',strtotime($from)));
    
    	return new CActiveDataProvider(SupplyItems::model(), array(
    		'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>9,
            ),            
    	));    
    }
    
    // find export money from date to date
    
    protected function _import($from,$to)
    {
        $criteria = new CDbCriteria;
        $criteria->condition='time >= :from AND time <= :to';
        $criteria->params=array(':to'=>date('Y-m-d',strtotime($to)), ':from'=>date('Y-m-d',strtotime($from)));
        $criteria->order = 'id desc';
    	return new CActiveDataProvider(Bill::model(), array(
    		'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>9,
            ),            
    	));
    }
    
    
    // find report chart for a month
    
    protected function _report($month)
    {
        $bill = Bill::model()->find(array(
                'limit' => '1',
            ));
                
        if($bill===null)
			$firstMonth = date('m-Y');
        else
            $firstMonth  = date('m-Y',strtotime($bill->time));        
        
        // Tim tat ca thang tu ngay bat dau log
        $arrMonths = array();
        $arrMonths[] = $firstMonth;
        while( strtotime( "01-" . $firstMonth ) < strtotime( "01-" . date('m-Y') ) )
        {  
            if(date('m',strtotime('01-' . $firstMonth)) == '12')
            {
                $firstMonth = '01-' . date('Y',strtotime('01-' . $firstMonth . " +1 year"));
            }
            else            
                $firstMonth = date('m-Y',strtotime('01-' . $firstMonth . " +1 month"));
            
            $arrMonths[] = $firstMonth;
        };
        
        // Tim ngay cua thang hien tai
        $totalDays = date('t', strtotime( "01-" . $month ) ); 
        $arrData = array();
        
        // Khoi tao du lieu cho tung ngay
        
        for ($index = 1; $index <= $totalDays; $index++) {
            $arrData[$index] = 0;
        }
        
        $days = Bill::model()->findAll(array(
            'condition' => 'time LIKE "'.date('Y-m',strtotime('01-'.$month)).'%"',
            )
        );
        
        foreach ($days as $day) {
            $current = (int) date('d',strtotime($day->time));
            $checkout = unserialize($day->data);            
            $arrData[$current] +=  $checkout['payment']/1000;
        }      
        
        $import = $this->_import('1-' . $month,$totalDays .'-'.$month);
        $total_money_import = 0;
        $items_import = Bill::model()->findAll($import->getCriteria());
        foreach($items_import as $item)
        {
            $money = unserialize($item->data);
            $total_money_import += $money['payment'];
        }
        
        $export = $this->_export('1-' . $month,$totalDays .'-'.$month);
        $total_money_export = 0;
        $items_export = SupplyItems::model()->findAll($export->getCriteria());
        foreach($items_export as $item)
        {
            $money = $item->price;
            $total_money_export += $money;
        }
		$this->render('index',
            array(
                'months' => $arrMonths,
                'currentMonth' => $month,
                'getMoney' => $arrData, 
                'import' => $import,
                'export' => $export,
                'money_import' => $total_money_import,
                'money_export' => $total_money_export,
            )        
        );
    }
    
}